package com.studentapp.fragment;


import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.google.gson.Gson;
import com.studentapp.R;
import com.studentapp.adapter.CommentAdapter;
import com.studentapp.bean.CommentGsonBean;
import com.studentapp.bean.LoginGsonBean;
import com.studentapp.bean.MyComment;
import com.studentapp.bean.OthersComment;
import com.studentapp.tool.MyURL;
import com.studentapp.tool.StudentSpTool;
import com.studentapp.tool.VolleyTool;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class CommentFragment extends Fragment {
    private ListView listView;
    private LoginGsonBean.DataBean studentBean;
    private static final String COMMENT_LIST = "/messages/list";

    public CommentFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //获取当前登录的学生的数据
        StudentSpTool studentSpTool = new StudentSpTool(getContext());
        studentBean = studentSpTool.getStudentBean();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_comment, container, false);
        listView = (ListView) view.findViewById(R.id.list_comment);
        initListView();

        return view;
    }

    //刷新listView
    public void initListView() {
        new Thread(){
            @Override
            public void run() {
                super.run();
                final List<Object> beans = new ArrayList<>();
                List<CommentGsonBean.DataBean> commentBean = getCommentList();

                //遍历判断当前评论是否属于当前登录的用户
                for (int i = 0; i < commentBean.size(); i++) {
                    CommentGsonBean.DataBean dataBean = commentBean.get(i);
                    if (dataBean.getId() == studentBean.getId()) {
                        beans.add(new MyComment("还没有头像", dataBean.getContent()));
                    } else {
                        beans.add(new OthersComment("还没有头像", dataBean.getContent()));
                    }
                }
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        listView.setAdapter(new CommentAdapter(getContext(),beans));
                    }
                });
            }
        }.start();
    }

    /**
     * 使用volley获取评论列表
     * @return 评论列表
     */
    public List<CommentGsonBean.DataBean> getCommentList() {
        String postResponse = VolleyTool.getPostResponse(getContext(), MyURL.DJP_URL + COMMENT_LIST);

        Gson gson = new Gson();

        CommentGsonBean commentGsonBean = gson.fromJson(postResponse, CommentGsonBean.class);

        return commentGsonBean.getData();
    }
}
